package com.tanhua.manager.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.manager.domain.Log;
import com.tanhua.manager.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Slf4j
@Component
@RocketMQMessageListener(topic = "tanhua-log", consumerGroup = "tanhua-log-consumer")
public class LogMessageListener implements RocketMQListener<String> {

    @Autowired
    private LogService logService;
    @Override
    public void onMessage(String message) {
        log.info("【MQ消息】收到消息，topic:{}, message:{}", "tanhua-log", message);
        Map map = JSON.parseObject(message, Map.class);
        Log logObj = new Log();
        logObj.setUserId(Long.valueOf(map.get("userId").toString()));
        logObj.setType((String) map.get("type"));
        logObj.setLogTime((String) map.get("date"));
        logObj.setCreated(new Date());
        logObj.setUpdated(new Date());
        logService.save(logObj);
    }
}
